package com.gw.quotes.activity;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gw.quotes.AppInfo;
import com.gw.quotes.R;
import com.gw.quotes.adapter.OrderAdapter;
import com.gw.quotes.adapter.OrderPagerAdapter;
import com.gw.quotes.entity.business.OrderInfoEntity;
import com.gw.quotes.entity.business.OrderListEntity.OrderItem;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.OrderInfoQueryServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.NoScrollListView;

public class MyOrderActivity extends BaseActivity {

	private ViewPager viewPager;// 页卡内容
	// private TextView textView1, textView2, textView3, textView4;
	private TextView textView2, textView3;
	private ImageView imageView;// 动画图片
	private List<Fragment> fragmens;// Tab页面列表
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	private int index;
	private ArrayList<OrderItem> orderlist;
	private List<View> list;
	private LayoutInflater inflater;

	private int pageSize = 10;// 页面大小

	int pageCur = 1;// 当前页

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_order);
		inflater = getLayoutInflater();
		InitImageView();
		getData();
		InitTextView();
		InitViewPager();
	}

	private void getData() {
		Intent intent = getIntent();
		orderlist = (ArrayList<OrderItem>) intent
				.getSerializableExtra("orderlist");
		index = intent.getIntExtra("index", 1);
		if (index == -1) {
			// 点击我的订单时，默认显示1.待确认，1.待确认为空时，显示2.已确认
			index = 1;
			for (OrderItem order : orderlist) {
				if (order.getStatus() == 1) {
					index = 0;
					break;
				}
			}
		}
		moveImageView(index);
	}

	private void InitViewPager() {
		viewPager = (ViewPager) findViewById(R.id.vPager);

		list = new ArrayList<View>();

		list.add(inflater.inflate(R.layout.item_order_viewpager, null));
		list.add(inflater.inflate(R.layout.item_order_viewpager, null));

		// list.add(inflater.inflate(R.layout.item_order_viewpager, null));
		// list.add(inflater.inflate(R.layout.item_order_viewpager, null));

		viewPager.setAdapter(new OrderPagerAdapter(list));
		viewPager.setCurrentItem(index);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());

		ListView listview = (ListView) list.get(index).findViewById(
				R.id.v0_listview);

		setListview(index, listview);
	}

	private void setListview(final int index, final ListView listview) {

		final LinearLayout normal = (LinearLayout) list.get(index)
				.findViewById(R.id.normal_layout);
		final LinearLayout order_page_layout = (LinearLayout) list.get(index)
				.findViewById(R.id.order_page_layout);
		Button order_page_previous = (Button) list.get(index).findViewById(
				R.id.order_page_previous);
		TextView order_page_all = (TextView) list.get(index).findViewById(
				R.id.order_page_all);
		TextView order_page_cur = (TextView) list.get(index).findViewById(
				R.id.order_page_cur);
		Button order_page_next = (Button) list.get(index).findViewById(
				R.id.order_page_next);

		ArrayList<OrderItem> list = new ArrayList<OrderItem>();
		// if (index == 0) {
		// list = orderlist;
		// } else {
		for (OrderItem order : orderlist) {
			if (index == 0 && order.getStatus() == 1) {
				list.add(order);
			}
			if (index == 1 && order.getStatus() == 2) {
				list.add(order);
			}
		}
		// }
		final ArrayList<OrderItem> tempList = list;
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				request(tempList.get((int) id).getOrderguid());

			}

		});

		if (list.size() > 0) {

			int count = list.size();

			int allPage = 0;

			if (count % pageSize == 0) {

				allPage = count / pageSize;

			} else {
				allPage = (count / pageSize) + 1;

			}

			ArrayList<OrderItem> item = new ArrayList<OrderItem>();

			for (int i = (pageCur - 1) * pageSize; i < pageCur * pageSize
					&& i < count; i++) {

				item.add(list.get(i));

			}

			OrderAdapter orderAdapter = new OrderAdapter(MyOrderActivity.this,
					R.layout.item_my_order, item);

			listview.setAdapter(orderAdapter);
			orderAdapter.notifyDataSetChanged();
			listview.setVisibility(View.VISIBLE);
			order_page_layout.setVisibility(View.VISIBLE);

			if (allPage == 1) {
				order_page_previous.setEnabled(false);
				order_page_next.setEnabled(false);
			} else {

				if (pageCur <= 1) {
					order_page_previous.setEnabled(false);
					order_page_next.setEnabled(true);
				} else {
					order_page_previous.setEnabled(true);
				}
				if (pageCur >= allPage) {
					order_page_previous.setEnabled(true);
					order_page_next.setEnabled(false);
				} else {
					order_page_next.setEnabled(true);
				}
			}

			order_page_all.setText("共" + allPage + "页");
			order_page_cur.setText("第" + pageCur + "页");
			order_page_previous.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					pageCur--;
					order_page_layout.setVisibility(View.GONE);
					setListview(index, listview);
				}
			});
			order_page_next.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					pageCur++;
					order_page_layout.setVisibility(View.GONE);
					setListview(index, listview);
				}
			});
			normal.setVisibility(View.GONE);
		} else {
			normal.setVisibility(View.VISIBLE);
			listview.setVisibility(View.GONE);
			order_page_layout.setVisibility(View.GONE);
		}

	}

	private void InitTextView() {
		// textView1 = (TextView) findViewById(R.id.text1);
		textView2 = (TextView) findViewById(R.id.text2);
		textView3 = (TextView) findViewById(R.id.text3);
		// textView4 = (TextView) findViewById(R.id.text4);
		// textView1.setOnClickListener(new MyOnClickListener(0));
		textView2.setOnClickListener(new MyOnClickListener(0));
		textView3.setOnClickListener(new MyOnClickListener(1));
		// textView4.setOnClickListener(new MyOnClickListener(3));
	}

	/**
	 * 2 * 初始化动画，这个就是页卡滑动时，下面的横线也滑动的效果，在这里需要计算一些数据 3
	 */

	private void InitImageView() {
		imageView = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.order_cursor).getWidth();// 获取图片宽度
		int screenW = AppInfo.SCREEN_WIDTH;// 获取分辨率宽度
		offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		imageView.setImageMatrix(matrix);// 设置动画初始位置
	}

	public void back(View v) {
		finish();
	}

	/**
	 * 
	 * 头标点击监听 3
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			viewPager.setCurrentItem(index);
		}

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		// int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		// int two = one * 2;// 页卡1 -> 页卡3 偏移量

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		public void onPageSelected(int arg0) {
			moveImageView(arg0);
			ListView listview = (ListView) list.get(arg0).findViewById(
					R.id.v0_listview);

			setListview(arg0, listview);
			// Log.i("xd", "hdindex " + arg0);
			/*
			 * 两种方法，这个是一种，下面还有一种，显然这个比较麻烦 Animation animation = null; switch
			 * (arg0) { case 0: if (currIndex == 1) { animation = new
			 * TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) {
			 * animation = new TranslateAnimation(two, 0, 0, 0); } break; case
			 * 1: if (currIndex == 0) { animation = new
			 * TranslateAnimation(offset, one, 0, 0); } else if (currIndex == 2)
			 * { animation = new TranslateAnimation(two, one, 0, 0); } break;
			 * case 2: if (currIndex == 0) { animation = new
			 * TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1)
			 * { animation = new TranslateAnimation(one, two, 0, 0); } break;
			 * 
			 * }
			 */

			/*
			 * Animation animation = new TranslateAnimation(one * currIndex, one
			 * arg0, 0, 0);// 显然这个比较简洁，只有一行代码。 currIndex = arg0;
			 * animation.setFillAfter(true);// True:图片停在动画结束位置
			 * animation.setDuration(300); imageView.startAnimation(animation);
			 * Toast.makeText(MyOrderActivity.this, "您选择了" +
			 * viewPager.getCurrentItem() + "页卡", Toast.LENGTH_SHORT).show();
			 */
		}

	}

	private void moveImageView(int index) {
		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		Animation animation = new TranslateAnimation(one * currIndex, one
				* index, 0, 0);// 显然这个比较简洁，只有一行代码。
		currIndex = index;
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		imageView.startAnimation(animation);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * item的网络请求
	 */
	private void request(String orderguid) {
		dialog.show();
		BaseServiceInter httpService = new OrderInfoQueryServiceImpl();
		Request request = new Request();
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		request.setOrderguid(orderguid);
		httpService.handler(request, new HttpCallBack<OrderInfoEntity>() {
			@Override
			public void onFinished(OrderInfoEntity response) {
				if (response != null
						&& response.getResult() == Response.RESULT_SUCCESS) {
					Intent intent = new Intent(MyOrderActivity.this,
							OrderDetailActivity.class);
					intent.putExtra("OrderInfoEntity", response);
					startActivity(intent);
				} else {
					CommonTools.showShortToast(MyOrderActivity.this, "请求失败！");
				}
				dialog.dismiss();
			}
		});
	}

}
